﻿using System;
using PTop.Code;
using PTop.Domain.Entity.SystemManage;
using PTop.Domain.IRepository.SystemManage;
using PTop.Repository.SystemManage;

namespace PTop.Application.SystemManage
{
    public class UserLogOnApp
    {
        private IUserRepository userService = new UserRepository();
        private IUserLogOnRepository service = new UserLogOnRepository();

        public UserLogOnEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void UpdateForm(UserLogOnEntity userLogOnEntity)
        {
            service.Update(userLogOnEntity);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userPassword"></param>
        /// <param name="keyValue"></param>
        public void RevisePassword(string userPassword, string keyValue)
        {
            UserLogOnEntity userLogOnEntity = GetForm(keyValue);
            userLogOnEntity.F_Id = keyValue;
            userLogOnEntity.F_UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
            userLogOnEntity.F_UserPassword = Md5.md5(DESEncrypt.Encrypt(Md5.md5(userPassword, 32).ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
            service.Update(userLogOnEntity);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        public UserEntity ChangePassword(string userName, string oldPassword, string newPassword)
        {
            UserEntity userEntity = userService.FindEntity(t => t.F_Account == userName);
            if (userEntity == null)
            {
                throw new Exception("账户不存在，请重新输入");
            }
            if (userEntity.F_EnabledMark != true)
            {
                throw new Exception("账户被系统锁定,请联系管理员");
            }
            //验证旧密码
            UserLogOnEntity userLogOnEntity = GetForm(userEntity.F_Id);
            string dbPassword = Md5.md5(DESEncrypt.Encrypt(oldPassword.ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
            if (dbPassword != userLogOnEntity.F_UserPassword)
            {
                throw new Exception("原密码不正确，请重新输入");
            }
            //修改密码
            userLogOnEntity.F_UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
            userLogOnEntity.F_UserPassword = Md5.md5(DESEncrypt.Encrypt(newPassword, userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
            service.Update(userLogOnEntity);
            return userEntity;
        }

        /// <summary>
        /// 找回密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="idCard"></param>
        /// <param name="newPassword"></param>
        public UserEntity FindPassword(string userName, string idCard, string newPassword)
        {
            UserEntity userEntity = userService.FindEntity(t => t.F_Account == userName);
            if (userEntity == null)
            {
                throw new Exception("账户不存在，请重新输入");
            }
            if (userEntity.F_EnabledMark != true)
            {
                throw new Exception("账户被系统锁定,请联系管理员");
            }
            if (string.IsNullOrEmpty(userEntity.F_IdCard))
            {
                throw new Exception("此账户未设置身份证号,请联系管理员");
            }
            //验证身份证后6位
            if (idCard != userEntity.F_IdCard.Substring(userEntity.F_IdCard.Length - 6))
            {
                throw new Exception("身份证号后6位不正确，请重新输入");
            }
            //修改密码
            UserLogOnEntity userLogOnEntity = GetForm(userEntity.F_Id);
            userLogOnEntity.F_UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
            userLogOnEntity.F_UserPassword = Md5.md5(DESEncrypt.Encrypt(newPassword, userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
            service.Update(userLogOnEntity);
            return userEntity;
        }
    }
}
